package com.cmsz.shuang.booter.rpc;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * SocketLinker 采用java socket进行网络通信，通过ObjectInputStream 和ObjectOutputStream 来序列化
 * @author shuangzh
 *
 */
public class SocketLinker implements Linker {
	Socket socket =null;
	ObjectInputStream in=null;
	ObjectOutputStream out=null;
	
	public static SocketLinker newClientSocketLinker(String address, int port) throws UnknownHostException, IOException{
		return new SocketLinker( new Socket(address, port));
	}
	
	public SocketLinker(Socket s) throws IOException{
		this.socket = s;
		out = new ObjectOutputStream(socket.getOutputStream());
		in = new ObjectInputStream(socket.getInputStream());
		
	}
	
	// 发送对象
	synchronized public void  sendObject(Object object) throws IOException {
		out.writeObject(object);
		out.flush();
	}
	
	// 接受对象
	synchronized public Object receiveObject() throws ClassNotFoundException, IOException {
		return in.readObject();
	}
}